﻿#region

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

using Pob.CustomEventArgs;
using Pob.GameObjects;

#endregion

namespace Pob.ScreenManagement
{
    public delegate void MenuItemPickedEvent(object obj, MenuItemPickedEventArgs args);

    public class MainMenuScreen : ScreenBase
    {
        #region Fields

        private readonly MenuControl _control;

        private readonly MenuItemGroup _menuItemGroup;

        private bool _firstUpdate = true;

        #endregion

        #region Constructors and Destructors

        public MainMenuScreen(Game game, MenuItemPickedEvent eventHandler)
            : base(game)
        {
            _menuItemGroup = new MenuItemGroup(game, GameContent.FontMenuItems, Color.White, drawStringMode: DrawStringMode.WithSelected);
            _menuItemGroup.Add("start", OnStartItem, true);
            _menuItemGroup.Add("options", OnOptionsItem);
            _menuItemGroup.Add("exit", OnExitItem);

            MyComponents.AddRange(_menuItemGroup.MenuItems);

            _control = new MenuControl(_menuItemGroup.MenuItems);
            MenuItemPicked += eventHandler;
        }

        #endregion

        #region Public Events

        public event MenuItemPickedEvent MenuItemPicked;

        #endregion

        #region Public Methods and Operators

        public override void Initialize()
        {
            _menuItemGroup.CalculatePositions();

            AddRange(MyComponents);

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            if (!_firstUpdate)
            {
                _control.Update(gameTime);
            }
            else
            {
                // Input-Abfrage beginnt erst, wenn keine Keys mehr aus dem vorherigen Screen gedrückt sind
                _firstUpdate = Keyboard.GetState().GetPressedKeys().Length > 0;
            }
        }

        #endregion

        #region Methods

        private void OnExitItem(object obj, MenuItemPickedEventArgs args)
        {
            if (MenuItemPicked != null)
            {
                MenuItemPicked(this, new MenuItemPickedEventArgs((MenuItem)obj)
                    {
                        MenuState = MenuStates.Exit
                    });
            }
        }

        private void OnOptionsItem(object obj, MenuItemPickedEventArgs args)
        {
            if (MenuItemPicked != null)
            {
                MenuItemPicked(this, new MenuItemPickedEventArgs((MenuItem)obj)
                    {
                        MenuState = MenuStates.Options
                    });
            }
        }

        private void OnStartItem(object obj, MenuItemPickedEventArgs args)
        {
            if (MenuItemPicked != null)
            {
                MenuItemPicked(this, new MenuItemPickedEventArgs((MenuItem)obj)
                    {
                        MenuState = MenuStates.Start
                    });
            }
        }

        #endregion
    }
}